require([
    'jquery',
    'module/handlebars',
    'global',
    'underscore',
    'bootstrap.typeahead',
    'bootstrap',
    'requirejs/domready!'], 
function($, handlebars, g, _, Typeahead) {
	
	
//	$('#search-input').select2({
//		placeholder: "请输入车型编码",
//		minimumInputLength: 1,
//		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
//	        url: g.context + '/search',
//	        dataType: 'json',
//	        quietMillis: 250,
//	        data: function (term, page) {
//	            return {
//	                q: term, // search term
//	            };
//	        },
//	        results: function (data, page) { // parse the results into the format expected by Select2.
//	            // since we are using custom formatting functions we do not need to alter the remote JSON data
//	            return { results: data.data };
//	        },
//	        cache: true
//	    },
//	    formatResult: formatSearchResult, // omitted for brevity, see the source of this page
//	    formatSelection: formatSearchResult,  // omitted for brevity, see the source of this page
//	    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
//	    escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
//	});
	
	$('#hot-brands').on('click', '.nav > li > a', function(){
		var href = $(this).attr('href');
		var category = $('#search-form input[name="category"]').filter(function(idx){
			return $(this).prop('checked');
		}).val();
		if(href.indexOf('category=')!=-1){
			$(this).attr('href',href.replace(/category=(\w)*/, 'category='+category));
		}else{
			$(this).attr('href',href+'?category='+category);
		}
		return true;
	});
	
	new Typeahead('#search-input', {
		autoSelect: false,
		delay: 500,
		source: function (query, process) {
            $.ajax({
                url: g.context + '/search/tip',
                type: 'GET',
                dataType: 'json',
                data: {q: query, category: $('input[name=category]:checked', '#search-form').val()},
                success: function(data) {
                    console.log(data);
                    process(data.data);
                }
            });
        },
        afterSelect: function(o){
        	if(!o) return;
        	var url = g.context;
        	if(o.type == 'vm'){
        		url += '/selection/' + o.id;
        	}else{
        		url += '/selection/' + o.parentId + '/products/' + o.id;
        	}
        	window.location.href = url;
        }
	});
	
});